<?php
require_once (__DIR__ . "/../inc/functions.php");
require_once (__DIR__ . "/Keywords.php");

class orderItems
{
	static function GetAll()
	{
		$conn = GetConnection();
		$results=$conn->query('SELECT * FROM orderItems');
		$error=$conn->error;
		return $results;
	}
	
	static function Get($id)
	{
		$conn = GetConnection();
		$results = $conn->query("SELECT * FROM orderItems WHERE id=$id");
		$row = $results->fetch_assoc();
		$error=$conn->error;
		return $row;
	}

	static function Insert($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);		
		$sql = 	"INSERT INTO orderItems (productId,quantity,orderId) "
			.	"Values ($row2[productId],$row2[quantity],$row2[orderId]); ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Update($row)
	{
		//print_r ($row);
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);			
		$sql = 	"UPDATE orderItems "
			.	"Set productId=$row2[productId],quantity=$row2[quantity],orderId=$row2[orderId] "
			.	"WHERE id=$row2[id] ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Delete($id)
	{
		$conn = GetConnection();
		$sql = 	"DELETE FROM orderItems "
			.	"WHERE id=$id ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;				
	}
	static function Validate($row)
	{
		$results = array();
		if(empty($row['quantity'])) $results['quantity'] = 'Quantity is required';		
		
		return count($results) > 0 ? $results : true;
	}
	
}

?>
